//
//  GTVProxyServer.m
//  GTVProxyServer
//
//  Created by gtv on 2018/2/28.
//  Copyright © 2018年 gtv. All rights reserved.
//

#import "GTVProxyServer.h"
#import "ProxyWrapper.h"

@interface GTVProxyServer() {
    
    ProxyWrapper * _wrapper;
}

@end

@implementation GTVProxyServer

#define PROXY_CODE_VER @"gtv proxy ver 0.2"

static GTVProxyServer * _instance = nil;

- (id) init
{
    self = [super init];
    if(self) {
        _wrapper = nil;
    }
    return self;
}

- (void)proxyWillStart:(ProxyWrapper *)polipo
{
    NSLog(@"proxyServerWillStart");
}

- (void)proxyWillStop:(ProxyWrapper *)polipo
{
    NSLog(@"proxyServerWillStop");
}

- (void)proxyDidStart:(ProxyWrapper *)polipo
{
    NSLog(@"proxyServerDidStart");
}

- (void)proxyDidStop:(ProxyWrapper *)polipo
{
    NSLog(@"proxyServerDidStop");
}

- (void)proxyDidTunnelStop:(ProxyWrapper *)polipo
{
    NSLog(@"proxyServerDidTunnelStop");
}

- (void)proxyDidFailWithError:(NSString *)error proxyWrapperInstance:(ProxyWrapper *)polipo
{
    NSLog(@"proxyServerDidFailWithError:%@", error);
}

- (void)proxyLogMessage:(NSString *)message
{
    NSLog(@"%@", message);
}

- (void) initAndStartProxy
{
    if( _wrapper == nil ) {
        
        _wrapper = [[ProxyWrapper alloc] initWithDelegate:_instance];
        [_wrapper start];
    }
}

- (void) clear
{
    [_wrapper clear];
}

+ (void) setListenPort:(int)p
{
    [ProxyWrapper setPort:p];
}

// 监听端口，默认3600
+ (int) getListenPort
{
    return [ProxyWrapper getPort];
}

+ (void) checkLaunchProxyServer
{
    NSLog(@"checkLaunchProxyServer %@ ", PROXY_CODE_VER);
    @synchronized([GTVProxyServer class]) {
        
        if( _instance == nil ) {
            _instance = [[GTVProxyServer alloc] init];
        }
        
        [_instance initAndStartProxy];
    }
}

+ (void) clearCache
{
    [_instance clear];
}

@end
